// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Copyright (C) 2008 Vignette Corporation  All rights reserved.
// THIS PROGRAM IS CONFIDENTIAL AND AN UNPUBLISHED WORK AND TRADE SECRET
// OF THE COPYRIGHT HOLDER, AND DISTRIBUTED ONLY UNDER RESTRICTION.
// EXCEPT AS EXPLICITLY STATED IN A WRITTEN AGREEMENT BETWEEN THE PARTIES,
// THE SOFTWARE IS PROVIDED AS-IS, WITHOUT WARRANTIES OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
// NONINFRINGEMENT, PERFORMANCE, AND QUALITY.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import com.vignette.community.client.services.common.RemoteObjectEntity;
import com.vignette.community.client.services.common.XAPIConstants;
import com.vignette.community.client.services.dataservice.Action;
import com.vignette.community.client.services.dataservice.ActionData;
import com.vignette.community.client.services.dataservice.CommunityDataServiceFactory;
import com.vignette.community.client.services.dataservice.CommunityDataServiceIf;
import com.vignette.community.client.services.dataservice.CreateNewAction;
import com.vignette.community.client.services.dataservice.Query;
import com.vignette.community.client.services.dataservice.RemoteObjectActionData;
import com.vignette.community.client.services.dataservice.RemoteObjectQuery;
import com.vignette.community.client.services.dataservice.RemoteObjectQueryCreate;
import com.vignette.community.client.services.dataservice.Response;
import com.vignette.community.client.services.dataservice.SimpleActionData;
import com.vignette.community.client.services.util.Util;

import javax.servlet.http.HttpServletResponse;

/**
 * Community Data example - Working with Ratings and Reviews. </br></br> For
 * this example to work the <code>vgncommsvcs.properties</code> should be found
 * in the classpath and should contain an entry based on the following: </br>
 * <code> CommunityServer1=collab,1,&lt;community_server_host&gt;,&lt;community_server_port&gt;,/xapi/,/xapi/,com.vignette.community.client.services.security.VssSecurityHandler,vssKey=changeme
 * </code>
 */
public class CDataRatingsAndReviews {

    public String createRemoteObject(CommunityDataServiceIf dservice,
                                     RemoteObjectEntity ro) {
        // Create a Remote Object

        String ROName = "What is the most popular sport?";
        String RODescription = "Story comparing popular sports";

        Action createAction = new CreateNewAction();
        ActionData remoteActionData =
                new RemoteObjectActionData(ROName,
                        RODescription,
                        ro);
        RemoteObjectQueryCreate createQuery =
                new RemoteObjectQueryCreate(ro,
                        createAction,
                        remoteActionData,
                        false);
        Response response1 = dservice.query(createQuery);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {
            return Util.getRedirectOid(response1);
        }
        return null;
    }

    private void addRatingToRemoteObject(CommunityDataServiceIf dservice,
                                         String oid) {
        Action createAction = new CreateNewAction();
        SimpleActionData simpleActionData = new SimpleActionData();
        simpleActionData.addParams(
                XAPIConstants.ObjectFieldName,
                oid);
        simpleActionData.addParams(
                "ratingKey",
                "4");
        simpleActionData.addParams(
                "type",
                "Rating");
        simpleActionData.addParams(
                "allowReview",
                "false");
        simpleActionData.addParams(
                "ratingDefintion",
                "1.89.1");
        Response response1 = dservice.execute(createAction, simpleActionData);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {
        }
    }

    private void getRemoteObjectsSortedByBestRated(
            CommunityDataServiceIf dservice,
            RemoteObjectEntity ro) {

        String extobjid = null;
        String extobjcontext = null;

        // This remoteObjectEntity will find all Remote Objects
        // from the realm and type from the supplied RO.  Object id and context
        // are not used in the search criteria.
        RemoteObjectEntity roEntity = new RemoteObjectEntity(extobjid,
                extobjcontext,
                ro.getExternalObjectSystemType(),
                ro.getExternalObjectSystemId(),
                ro.getExternalObjectRealm(),
                ro.getExternalObjectType());

        String pageSize = "20";
        String pageNum = "1";

        Query query = new RemoteObjectQuery(roEntity);
        query.addAdditionalParam(
                XAPIConstants.INCLUDES,
                XAPIConstants.RATINGS);
        query.addAdditionalParam(
                XAPIConstants.SORTING,
                XAPIConstants.SORT_BY_RATINGS_DEFN + "1.89.1");
        query.addAdditionalParam(
                XAPIConstants.SORT_DIRECTION,
                XAPIConstants.SORT_DIRECTION_DESC);
        query.addAdditionalParam(
                XAPIConstants.PAGINATION,
                pageSize);
        query.addAdditionalParam(
                XAPIConstants.PAGINATION,
                pageNum);
        Response response1 = dservice.query(query);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_OK) {
            String responseXML = response1.getData();
            // Parse out the tags from this XML response.
        }

    }

    private void addReviewToRemoteObject(CommunityDataServiceIf dservice, String oid) {
        Action createAction = new CreateNewAction();
        SimpleActionData simpleActionData = new SimpleActionData();
        simpleActionData.addParams(
                XAPIConstants.ObjectFieldName,
                oid);
        simpleActionData.addParams(
                "ratingKey",
                "4");
        simpleActionData.addParams(
                "type",
                "Review");
        simpleActionData.addParams(
                "allowReview",
                "true");
        simpleActionData.addParams(
                "ratingDefintion",
                "1.89.1");
        simpleActionData.addParams(
                "content",
                "The remote Object is really cool");
        Response response1 = dservice.execute(createAction, simpleActionData);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {
        }
    }

    public static void main(String[] args) {

        String communityServerId = "CommunityServer1";
        String currentUser = "admin";
        String password = null;

        CommunityDataServiceIf dservice =
                CommunityDataServiceFactory.getInstance()
                        .createCommunityDataService(communityServerId,
                                currentUser,
                                password);

        // Create a Remote Object
        String extobjid = "12345";
        String extobjcontext = "Sports Channel";
        String extsystemid = "1";
        String extsystemtype = "CMS";
        String extrealm = "News Site";
        String extobjtype = "Article";
        String otherval = XAPIConstants.EXT_OBJ_STATUS
                + "="
                + XAPIConstants.DEV_STATUS;

        RemoteObjectEntity ro = new RemoteObjectEntity(extobjid,
                extobjcontext, extsystemtype,
                extsystemid, extrealm, extobjtype,
                otherval);

        CDataRatingsAndReviews cdata = new CDataRatingsAndReviews();

        String oid = cdata.createRemoteObject(dservice, ro);
        cdata.addRatingToRemoteObject(dservice, oid);
        cdata.addReviewToRemoteObject(dservice, oid);
        cdata.getRemoteObjectsSortedByBestRated(dservice, ro);

    }


}
